<?php
namespace App\Logic;

use Validator;
use App\Models\Message;
use App\Models\User;
use App\MessageModel;


class MessageLogic 
{
    
    //测试模型
    public function messageAll()
    {
        $messageModel = new Message;
       
        $messageAll = $messageModel->messageAll();
        
        return $messageAll;
    }
    
    /**
     * 留言列表
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['direction'] 手势行为
     * @param $param['from'] 锚点
     * @param $param['fetch'] 每页取几条，默认10
     * @param $param['ctxtype'] 内容类型；''-全部，0-留言
     * @param $param['userid'] 操作者
     * 
     * @return array
     */
    public function messageList($param)
    {
        //方便模拟
        $param['direction'] = !empty($param['direction']) ? (int)$param['direction'] : 1;//手势行为
        $param['from'] = !empty($param['from']) ? (int)$param['from'] : 0;//锚点
        $param['fetch'] = !empty($param['fetch']) ? (int)$param['fetch'] : 10;//每页取几条，默认10
        $param['ctxtype'] = isset($param['ctxtype']) ? (int)$param['ctxtype'] : '';//内容类型；''-全部，0-留言
        $param['userid'] = !empty($param['userid']) ? (int)$param['userid'] : 0;//操作者
        
        $messageModel = new Message();
        
        //列表数据
        $messageList = $messageModel->messageList($param);
        if ($messageList) {
            $str = '';
            foreach ($messageList['res'] as $key => $val) {
                if ($val->nickname) {
                    $messageList['res'][$key]->nickname = urldecode($val->nickname);
                } else {
                    $messageList['res'][$key]->nickname = $val->phone;
                }
                
                $messageList['res'][$key]->title = urldecode($val->title);
                $messageList['res'][$key]->createtime = $this->wordTime($val->createtime);
                
                for($i = 1; $i < 10; $i++) { 
                    $str = 'pic'.$i;
                    if (!empty($val->$str)) {
                        $messageList['res'][$key]->$str = urldecode($val->$str);
                    } else {
                        unset($messageList['res'][$key]->$str);
                    }
                }
            }
            
            return array('code' => 200, 'msg' => '留言列表~', 'data' => $messageList);   
        }else {
            return array('code' => 201, 'msg' => '请求失败');   
        }
    }
    
    /**
     * 时间转换
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $val 要转换的时间戳
     * 
     * @return $str
     */
    function wordTime($time) 
    {
        $time = (int) substr($time, 0, 10);
        $int = time() - $time;
        $str = '';
        if ($int <= 30) {
            $str = sprintf('刚刚', $int);
        } elseif ($int < 60){
            $str = sprintf('%d秒前', $int);
        } elseif ($int < 3600){
            $str = sprintf('%d分钟前', floor($int / 60));
        } elseif ($int < 86400){
            $str = sprintf('%d小时前', floor($int / 3600));
        } elseif ($int < 2592000){
            $str = sprintf('%d天前', floor($int / 86400));
        } else {
            $str = date('Y-m-d H:i:s', $time);
        }
        
        return $str;
    }
    
    /**
     * 留言
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $param['phone'] 账号
     * @param $param['accesstoken'] 操作令牌
     * @param $param['title'] 留言内容
     * 
     * @return array
     */
    public function sendMsg($param)
    {
        //方便模拟
        $param['phone'] = !empty($param['phone']) ? $param['phone'] : '';//账号
        $param['accesstoken'] = !empty($param['accesstoken']) ? $param['accesstoken'] : '';//操作令牌
        $param['title'] = !empty($param['title']) ? $param['title'] : '';//操作令牌
        $param['pic1'] = !empty($param['pic1']) ? $param['pic1'] : '';//图片pic1~9
        $param['pic2'] = !empty($param['pic2']) ? $param['pic2'] : '';//图片pic1~9
        $param['pic3'] = !empty($param['pic3']) ? $param['pic3'] : '';//图片pic1~9
        $param['pic4'] = !empty($param['pic4']) ? $param['pic4'] : '';//图片pic1~9
        $param['pic5'] = !empty($param['pic5']) ? $param['pic5'] : '';//图片pic1~9
        $param['pic6'] = !empty($param['pic6']) ? $param['pic6'] : '';//图片pic1~9
        $param['pic7'] = !empty($param['pic7']) ? $param['pic7'] : '';//图片pic1~9
        $param['pic8'] = !empty($param['pic8']) ? $param['pic8'] : '';//图片pic1~9
        $param['pic9'] = !empty($param['pic9']) ? $param['pic9'] : '';//图片pic1~9
        
        //数据校验
        $messages = [
            'phone.required' => '账号不能为空',
            'accesstoken.required' => '操作令牌不能为空'
        ];
        
        $validator = Validator::make($param, [
            'phone' => 'required',
            'accesstoken' => 'required'
        ], $messages);
        
        if ($validator->fails()) {
            $errors = $validator->errors();
            $msg = $errors->first();
            
            return $this->errorMsg($errors, $msg);  
        }
        
        $content = $param['title'];
        for ($i = 1; $i < 10; $i++) {
            $content .= $param['pic'.$i];
        }
        
        if (empty($content)) {
            return array('code' => 204, 'msg' => '留言内容不能为空~');
        }
        
        $userModel = new User();
        
        //登录校验与黑名单校验
        //操作令牌校验
        $getToken = $userModel->getToken($param);
        if (empty($getToken)) {
            return array('code' => 205, 'msg' => '您还没有登录哟~');
        } else if ($getToken->status!=1) {//黑名单校验
            return array('code' => 206, 'msg' => '您没有权限留言，请与管理员联系~');
        }
        
        //留言
        $param['usertype'] = $getToken->usertype;
        $param['userid'] = $getToken->userid;
        
        $messageModel = new Message();
        
        $sendMsg = $messageModel->sendMsg($param);
        if ($sendMsg['code'] == 200) {
            return array('code' => 200, 'msg' => '留言成功~', 'data' => $sendMsg);
        } else {
            return array('code' => 201, 'msg' => '留言失败~');
        }
    }
    
    /**
     * 留言信息校验
     * 
     * @author 谢望鑫 <xiewxin@addcn.com>
     * @param $errors 错误处理对象
     * @param $msg 错误提示信息
     * 
     * @return array('code'=>$code,//状态码
     *               'msg'=>$msg,//提示语
     *               );
     */
    public function errorMsg($errors, $msg)
    {
        if ($errors->has('phone')) {
            $status = 202;
        } else if ($errors->has('accesstoken')) {
            $status = 203;
        } else if ($errors->has('title')) {
            $status = 204;
        }
        
        return array('code' => $status, 'msg' => $msg);
    }
   
}